package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.mapper.ConcurrentHotMapper;
import com.gsta.cdn.cdr.mapper.ConcurrentMapper;
import com.gsta.cdn.cdr.service.ConcurrentHotService;
import com.gsta.cdn.cdr.service.ConcurrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/ConcurrentHot")
public class ConcurrentHotController {
    @Autowired
    private ConcurrentHotMapper concurrentHotMapper;

    @Resource
    private ConcurrentHotService concurrentHotService;

    //热度数统计
    @RequestMapping(path = "/ConcurrentHotHotNum", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ChannelConcurrent(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotHotNum(date,percent);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
//            resultEntity.setData(vodlist);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度数据统计
    @RequestMapping(path = "/ConcurrentHotData", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotData(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          @RequestParam(required = false,defaultValue = "1")int page,
                                          @RequestParam(required = false,defaultValue = "200")int pageSize,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotData(date,percent,page,pageSize);
        int total = concurrentHotMapper.ConcurrentHotDataCount(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度地区列表
    @RequestMapping(path = "/ConcurrentHotArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotArea(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotArea(date,percent);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
//            resultEntity.setData(vodlist);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度地区PopName列表
    @RequestMapping(path = "/ConcurrentHotAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotAreaPopName(@RequestParam(required = true) String date,
                                                 @RequestParam(required = true) String area,
                                          @RequestParam(required = true) String percent,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotAreaPopName(date,percent,area);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
//            resultEntity.setData(vodlist);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区热度PopName数据统计
    @RequestMapping(path = "/ConcurrentHotByAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotByAreaPopName(@RequestParam(required = true) String date,
                                                   @RequestParam(required = true) String area,
                                                   @RequestParam(required = true) String popName,
                                                  @RequestParam(required = true) String percent,
                                                  @RequestParam(required = false,defaultValue = "1")int page,
                                                  @RequestParam(required = false,defaultValue = "200")int pageSize,
                                                  HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotByAreaPopName(date,area,popName,percent,page,pageSize);
        int total = concurrentHotMapper.ConcurrentHotByAreaPopNameCount(date,area,popName,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区热度数据统计
    @RequestMapping(path = "/ConcurrentHotByArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotByArea(@RequestParam(required = true) String date,
                                                   @RequestParam(required = true) String area,
                                                   @RequestParam(required = true) String percent,
                                                   @RequestParam(required = false,defaultValue = "1")int page,
                                                   @RequestParam(required = false,defaultValue = "200")int pageSize,
                                                   HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotByArea(date,area,percent,page,pageSize);
        int total = concurrentHotMapper.ConcurrentHotByAreaCount(date,area,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //三级节点热度数据统计
    @RequestMapping(path = "/ConcurrentHotByPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentHotByPopName(@RequestParam(required = true) String date,
                                                   @RequestParam(required = true) String popName,
                                                   @RequestParam(required = true) String percent,
                                                   @RequestParam(required = false,defaultValue = "1")int page,
                                                   @RequestParam(required = false,defaultValue = "200")int pageSize,
                                                   HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentHotService.ConcurrentHotByPopName(date,popName,percent,page,pageSize);
        int total = concurrentHotMapper.ConcurrentHotByPopNameCount(date,popName,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }
}
